package dao.mysql;


import java.sql.*;
import java.util.*;

import dao.BookingDAO;


import model.BookingDTO;


public class BookingDAOImpl implements BookingDAO {

	private static final String dbUrl = "jdbc:mysql://localhost:3306/fbs";
	private static final String dbUserName = "root";
	private static final String dbPassword = "password";
	
	/* (non-Javadoc)
	 * @see dao.mysql.BookingDAO#findCurrentBookings()
	 */
	@Override
	public ArrayList<BookingDTO> findCurrentBookings() {
		ArrayList<BookingDTO> items = new ArrayList<BookingDTO>();
		try {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		String selectSql = "SELECT * FROM booking WHERE YEAR(FromDate) = YEAR(CURDATE()) AND MONTH(FromDate) = MONTH(CURDATE());";
		Connection conn = null;
		
		try {
			conn = DriverManager.getConnection(dbUrl, dbUserName, dbPassword);
			Statement st = conn.createStatement();
			ResultSet rs = st.executeQuery(selectSql);
			
			while (rs.next()) {
				BookingDTO booking = new BookingDTO();
				booking.setBookingID(rs.getInt("BookingID"));
				booking.setFacilityName(rs.getString("FacilityName"));
				booking.setStaffID(rs.getString("StaffID"));
				booking.setStatus(rs.getString("Status"));
				booking.setFromDate(rs.getDate("FromDate"));
				booking.setToDate(rs.getDate("ToDate"));
				booking.setPriorityID(rs.getInt("PriorityID"));
				booking.setBookingDate(rs.getDate("BookingDate"));
				booking.setReason(rs.getString("Reason"));

				items.add(booking);
			}
			st.close();
		} 
		
		catch (Exception e) {
			e.printStackTrace();
		} 
		
		finally {
			try {
				conn.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return items;
	}
}
